﻿using ExChange.Model;
using System.Data.Entity;
using System.Web.Mvc;
using System.Linq;
using System;


namespace ExChange.Web.Controllers
{
    public class MemberController : BaseController
    {
        public JFDHEntities entity = new JFDHEntities();
        public ActionResult Index()
        {
            return View();
        }


        public ActionResult GetAllLevel()
        {
            var list = entity.Member_Ware.Select(f => new { f.ID, f.Memberlevel }).ToList();
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetList(int page, int rows, string KeyWord = "", int duihuan = -1)
        {
            //var list = entity.Member.Include("Member_Ware").ToList();
            var list = from o in entity.Member.Include("Member_Ware") select o;
            if (duihuan == 2)
            {
                list = list.Where(f => f.IsValid == false);
            }
            else
            {
                list = list.Where(f => f.IsValid == true);
            }
            if (!string.IsNullOrEmpty(KeyWord))
            {
                list = list.Where(f => f.Phone.Contains(KeyWord) || f.MemberName.Contains(KeyWord));
            }
            if (duihuan != -1)
            {
                var isactive = duihuan == 1 ? true : false;
                list = list.Where(f => f.IsActive == isactive);
            }
            var totalcount = list.Count();
            var data = list.OrderByDescending(f => f.IsValid).ThenByDescending(f => f.ID).Skip((page - 1) * rows).Take(rows).ToList().Select(f => new
            {
                MemberName = f.MemberName + (f.IsValid.HasValue && f.IsValid.Value ? "" : "<i style='color:red'>已删除<i/>"),
                Memberlevel = f.Member_Ware == null ? "暂无" : f.Member_Ware.Memberlevel,
                f.Phone,
                f.loginname,
                f.password,
                f.ID,
                f.Codes,
                f.IsValid,
                f.IsActive,
                IsActiveText = f.IsActive.Value == true ? "已兑换" : "未兑换",
                f.Levels,
            });
            return Json(new { rows = data, total = totalcount }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Save(Member model)
        {
            //string pathList = Request["pathList"]; 
            if (model.ID == 0)
            {
                var isok = entity.Member.FirstOrDefault(f => f.Phone == model.Phone && f.IsValid.Value == true);
                if (isok != null)
                {
                    return Json(new JsonData.Messager() { msg = "操作失败,已存在该会员手机号！" });
                }
                Random rad = new Random();//实例化随机数产生器rad； 
                int value = rad.Next(1000, 10000);//用rad生成大于等于1000，小于等于9999的随机数；
                model.Codes = value.ToString(); //用label控件label1显示产生的四位随机数；
                model.IsActive = false;
                model.IsValid = true;
                entity.Member.Add(model);
            }
            else
            {
                entity.Entry<Member>(model).State = EntityState.Modified;
            }
            int row = entity.SaveChanges();
            return row > 0 ? Json(new JsonData().Result(true), JsonRequestBehavior.AllowGet) : Json(new JsonData().Result(false));
        }

        public ActionResult Del(int id)
        {
            var model = entity.Member.FirstOrDefault(f => f.ID == id);
            model.IsValid = false;
            //entity.Member.Remove(model);
            int row = entity.SaveChanges();
            return row > 0 ? Json(new JsonData().Result(true), JsonRequestBehavior.AllowGet) : Json(new JsonData().Result(false));
        }

    }
}
